package com.mcms.service.fz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ralasafe.user.User;

import com.mcms.service.dw.QueryCustomService;
import com.mcms.util.DBHelper;
import com.mcms.util.MapUtil;
import com.mcms.util.McmsApiConfig;
import com.mcms.web.vo.PageAction;

public class QueryFZCustomService {

	public static Map execute(Map sessionMap, Map paramMap) {

		boolean isSuc = true;
		String flag = MapUtil.getString(paramMap, "flag"); // 检索类型：1-初始检索 2-检索 3-高级检索
		Map map = new HashMap();

		QueryFZCustomService queryCustomFZ = new QueryFZCustomService();
		if ("2".equals(flag)) {

			// 检索：接诊医生为本人的，再加检索中的条件的客户信息列表
			map = queryCustomFZ.getConditionList(sessionMap, paramMap);
		} else if ("3".equals(flag)) {

			// 高级检索：接诊医生为本人的，再加高级检索中的条件的客户信息列表
			map = queryCustomFZ.getSeniorList(sessionMap, paramMap);
		} else {

			// 初始检索：接诊医生为本人的，分诊时间为当天的(“客户医生关系表”关联“客户信息表”)的客户信息列表
			map = queryCustomFZ.getDefaultList(sessionMap, paramMap);
		}

		// 返回查询结果列表
		return map;

	}

	/**
	 * 获取分诊列表初始记录
	 * 
	 * @return
	 */
	private Map getDefaultList(Map sessionMap, Map paramMap) {
		Map map = new HashMap();
		List list = new ArrayList();
		PageAction pageAction = (PageAction) paramMap.get("pageAction");
		if(pageAction == null){
			pageAction = new PageAction();
		}
		
		User user = (User)MapUtil.getObject(sessionMap, "mcmsUser");//获取ralasafe中的user信息
		String userId = user.getId().toString();//获取userId
		
		// 根据查询条件查询总条数
		String sqlStr = "select t.CUSTOM_ID from view_custom_base_info t where 1=2 ";
		
		String pageNum = MapUtil.getString(paramMap, "pageNum");// 起始记录
		String numPerPage = MapUtil.getString(paramMap, "numPerPage");// 每页展示多少条
		if("".equals(pageNum) || pageNum == null){
			pageNum = "1";
		}
		
		if("".equals(numPerPage) || numPerPage == null){
			numPerPage = "10";
		}
		Integer lp = Integer.parseInt(pageNum);
		Integer ln = Integer.parseInt(numPerPage);

		Integer beginNum = (lp - 1) * ln;
		Integer endNum = lp * ln;
		
		// 分页查询
		String sqlStrFy = "select t.CUSTOM_ID,t.CUSTOM_NAME,t.TELEPHONE_NO,t.subscribe_doctor_id,t.GENDER,t.QQ" +
				",case when t.doctor_status is not null then t.doctor_status when t.doctor_status is null and t.s_status is not null then t.s_status else 'a' end status " +
				"from view_custom_base_info t where 1=2 limit "+beginNum+","+endNum;
		
		//数据库查询
		try {
			DBHelper dbh = DBHelper.getInstance();
			list = dbh.query(sqlStr);// 查询总数
			if ( list == null ) {
				pageAction.getPagePara().setTotal(0);
			} else {
				pageAction.getPagePara().setTotal(list.size());
			}
			
			list = dbh.query(sqlStrFy);// 分页查询

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//将结果数据保存到结果map中
		pageAction.setNumPerPage(ln);
		pageAction.setPageNum(lp);
		map.put("pageAction", pageAction);
		map.put("resultList", list);
		
		return map;
	}

	/**
	 * 获取分诊列表条件检索记录
	 * 
	 * @return
	 */
	private Map getConditionList(Map sessionMap, Map paramMap) {
		Map map = new HashMap();
		List list = new ArrayList();
		PageAction pageAction = (PageAction) paramMap.get("pageAction");
		if(pageAction == null){
			pageAction = new PageAction();
		}
		
		User user = (User)MapUtil.getObject(sessionMap, "mcmsUser");//获取ralasafe中的user信息
		String userId = user.getId().toString();//获取userId
		
		// 根据查询条件查询总条数
		String sqlStr = "select t.CUSTOM_ID,t.CUSTOM_NAME,t.TELEPHONE_NO,t.subscribe_doctor_id,t.GENDER,t.QQ,case when t.doctor_status is not null then t.doctor_status when t.doctor_status is null and t.s_status is not null then t.s_status else 'a' end status from view_custom_base_info t where 1=1 ";
		
		// 拼接查询条件
		StringBuffer sb = new StringBuffer();
		String customInfo = MapUtil.getString(paramMap, "customInfo");
		String connect = MapUtil.getString(paramMap, "connect");
//		String customInfo = request.getParameter("customInfo");
//		String connect = request.getParameter("connect");

		if (customInfo != null && !"".equals(customInfo)) {
			sb.append(" and ( custom_name='" + customInfo + "' or SUBSCRIBE_DOCTOR_ID='" + customInfo
					+ "' or id_code='" + customInfo + "') ");
		}

		if (connect != null && !"".equals(connect)) {
			sb.append(" and ( qq='" + connect + "' or email='" + connect
					+ "' or TELEPHONE_NO='" + connect
					+ "' or FIXED_TELEPHONE_NO='" + connect + "') ");
		}
		
		sqlStr = sqlStr + sb.toString();
		
		// 分页查询条件
		String pageNum = MapUtil.getString(paramMap, "pageNum");// 起始记录
		String numPerPage = MapUtil.getString(paramMap, "numPerPage");// 每页展示多少条
		if("".equals(pageNum) || pageNum == null){
			pageNum = "1";
		}
		
		if("".equals(numPerPage) || numPerPage == null){
			numPerPage = "10";
		}
		Integer lp = Integer.parseInt(pageNum);
		Integer ln = Integer.parseInt(numPerPage);

		Integer beginNum = (lp - 1) * ln ;//begin num
		Integer endNum = lp * ln;//end num
		
		// 数据库查询
		try {
			DBHelper dbh = DBHelper.getInstance();
			list = dbh.query(sqlStr);// 查询总数
			if ( list == null ) {
				pageAction.getPagePara().setTotal(0);
			} else {
				pageAction.getPagePara().setTotal(list.size());
			}
			sb.append(" limit "+beginNum+", "+endNum);
			sqlStr = sqlStr + sb.toString();
			list = dbh.query(sqlStr);// 分页查询

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 将结果数据保存到结果map中
		pageAction.setNumPerPage(ln);
		pageAction.setPageNum(lp);
		map.put("pageAction", pageAction);
		map.put("resultList", list);
		
		return map;
	}

	/**
	 * 获取分诊列表高级检索记录
	 * 
	 * @return
	 */
	private Map getSeniorList(Map sessionMap, Map paramMap) {
		Map map = new HashMap();

		return map;
	}
}
